var httpUtil = require('../../utils/httpUtil.js');
// pages/me/me.js
Page({

  /**
   * 页面的初始数据
   */

  data: {
    hidden: false, //加载
    outline: false, //有无网络
    noData:false,
    default: 0,
    list: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // that.post()
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  sure:function(e){
    wx.redirectTo({
      url: '/pages/goodsDetail1/goodsDetail1?id=' + e.currentTarget.dataset.id,
    })
  },
  post: function() {
    var that = this;
    that.setData({
      hidden:true
    })
    var req = {
      userid: wx.getStorageSync("userId"),
      token: wx.getStorageSync('token')
    }
    httpUtil.post("/crm/usershopaddresslist", req)
      .then(function(res) {
        if (res.data.status == 200) {
          that.setData({
            hidden: false
          })
          var arr = [];
          for (var i = 0; i < res.data.data.msg.length; i++) {
            arr.push(res.data.data.msg[i])
            if (res.data.data.msg[i].isdefault == '是') {
              wx.setStorageSync('addressId', res.data.data.msg[i].id)
            }
          }
          that.setData({
            list: arr,
            noData:false
          })
          if(that.data.list.length==0){
            that.setData({
              noData: true
            })
          }
        } else {
          wx.showToast({
            title: '' + res.data.message + '',
          })
        }
      }).catch(function(res) {
        console.log(res);
      });
  },
  // 添加新地址
  add: function() {
    wx.navigateTo({
      url: '/pages/add/add',
    })
  },
  edit: function(e) {
    wx.navigateTo({
      url: '/pages/edit/edit?id=' + e.currentTarget.dataset.id,
    })
  },
  // 删除地址
  del: function(e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定要删除该地址吗？',
      success: function(sm) {
        if (sm.confirm) {
          var req = {
            id: e.currentTarget.dataset.id
          }
          httpUtil.post("/crm/usershopaddressdel", req)
            .then(function(res) {
              if (res.data.status == 200) {
                wx.showToast({
                  title: '' + res.data.message + '',
                })
                that.post()
              }
            }).catch(function(res) {
              console.log(res);
            });
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    //获取当前网络状态
    wx.getNetworkType({
      success: function(res) {
        // 返回网络类型, 有效值：
        // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
        if (res.networkType == 'none' || res.networkType == 'unknown') {
          that.setData({
            outline: true
          })
        } else {
          wx.removeStorageSync('addressId')
          that.setData({
            outline: false,
          })
          that.post()
        }
      }
    })

    //监听网络状态
    wx.onNetworkStatusChange(function(res) {
      if (res.networkType == 'none' || res.networkType == 'unknown') {
        that.setData({
          outline: true
        })
      } else {
        wx.removeStorageSync('addressId')
        that.setData({
          outline: false,
        })
        that.post()
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
})